class Solution {
    vector<vector<int>> ret;
    vector<int> path;
    bool used[8] = {0};

    void dfs(vector<int>& nums)
    {
        if (path.size() == nums.size())
        {
            ret.push_back(path);
            return;
        }

        for (int i = 0; i < nums.size(); i++)
        {
            if (i != 0 && nums[i] == nums[i - 1] && !used[i - 1])
            {
                //printf(":i = %d, nums[i] = %d, nums[i-1] = %d\n", i, nums[i], nums[i - 1]);
                continue;
            }

            if (!used[i])
            {
                used[i] = true;
                path.push_back(nums[i]);
                //printf("push: <%d, %d>\n", i, nums[i]);
                dfs(nums);
                path.pop_back();
                used[i] = false;
            }
        }
    }
public:
    vector<vector<int>> permuteUnique(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        dfs(nums);
        return ret;
    }   
};